﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Zore.App.Shared.Helpers.Invoker
{
    public class FormDataInvoker : InvokerContributor
    {
        public FormDataInvoker(string baseAddress) : base(baseAddress)
        {

        }

        public override async Task<TReturn> InnerInvokeWithReturn<TReturn, TData>(
            string requestUri, HttpMethod method, TData data = null)
        {
            var result = default(TReturn);

            var handler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
            };

            using (var client = new HttpClient(handler))
            {
                using (var request = BuildHttpRequestMessage(requestUri, method, data))
                {
                    try
                    {
                        client.BaseAddress = BaseAddress;
                        var msg = await client.SendAsync(request);
                        if (msg.StatusCode == HttpStatusCode.OK)
                        {
                            var strResult = await msg.Content.ReadAsStringAsync();
                            result = JsonConvert.DeserializeObject<TReturn>(strResult);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(
                            $"Error:{ex.Message} Inner Error:{ex.InnerException.Message}");
                    }
                    return result;
                }
            }
        }

        public override async Task InnerInvokeWithoutReturn<TData>(
            string requestUri, HttpMethod method, TData data = null)
        {
            var handler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
            };

            using (var client = new HttpClient(handler))
            {
                using (var request = BuildHttpRequestMessage(requestUri, method, data))
                {
                    client.BaseAddress = BaseAddress;
                    var msg = await client.SendAsync(request);
                }
            }
        }

        public override HttpRequestMessage BuildHttpRequestMessage<TData>(
            string requestUri, HttpMethod method, TData data = null)
        {
            var parameters = data as Dictionary<string, string>;

            var request = new HttpRequestMessage(method, requestUri);
            if (data != null)
            {
                var content = new MultipartFormDataContent();
                foreach (var item in parameters)
                {
                    content.Add(new StringContent(item.Value), item.Key);
                }
                request.Content = content;
            }
            return request;
        }
    }
}
